from tools.project_tool import sys_exit


def value_in_list(x, the_list, **kwargs):
    name = kwargs.pop('name', '')
    throw_it = kwargs.pop('throw_it', '')
    if not isinstance(the_list, list):
        sys_exit('需要传入一个list作比较，却得到：%s' % the_list, throw_it=True)
    if x not in the_list:
        sys_exit(
            '所给' + (name if name else '值') + f'需要为{the_list}其中一个，却得到：{x}',
            throw_it=throw_it
        )


def all_value_in_list(x, the_list, **kwargs):
    name = kwargs.pop('name', '')
    throw_it = kwargs.pop('throw_it', '')
    if not all(i in the_list for i in x):
        sys_exit(err_msg='所给' + (name if name else '值') + f'需要传入{the_list}中的一个或多个，却得到{x}', \
                 throw_it=throw_it)


def value_is_instance(x, o_type, **kwargs):
    throw_it = kwargs.pop('throw_it', '')
    if not isinstance(x, o_type):
        sys_exit(
            err_msg=f'类型错误，所给值需要为：{o_type}，却得到：{x.__class__}',
            throw_it=throw_it
        )


def same_len(a, b, **kwargs):
    throw_it = kwargs.pop('throw_it', '')
    l_b = len(b)
    l_a = len(a)
    if l_a != l_b:
        sys_exit(
            err_msg=f'希望A、B数组长度相同，却得到：A长{l_a}！=B长{l_b}',
            throw_it=throw_it
        )


if __name__ == '__main__':
    all_value_in_list(x=[1, 2], the_list=[2, 3], name='test', throw_it=False)
